package zhizhemodi.tech.short_term_bwork.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import zhizhemodi.tech.short_term_bwork.entity.Plan;
import zhizhemodi.tech.short_term_bwork.service.PlanService;
import zhizhemodi.tech.short_term_bwork.tool.CommonResult;

@RestController
@RequestMapping("plan")
public class PlanController {
    
    // 默认每页条数
    @Value("${default.page.rowNum}")
    private Integer ROW_MAX;

    @Resource
    PlanService planService;

    @GetMapping("list")
    public CommonResult<?> getPlanList(
        @RequestParam("departmentId") Integer dId,
        @RequestParam(required = false, name = "limit") Integer rowNum,
        @RequestParam(required = false, name = "page") Integer pageNum
    ) {
        List<Plan> plans = null;
        try {
            if(rowNum < 0) rowNum = ROW_MAX;
            if(pageNum < 0) pageNum = 0;
            plans = planService.getPlanList(dId, rowNum, pageNum);
        } catch (Exception e) {
            CommonResult.failed(e.getMessage());
        }
        return CommonResult.success(plans);
    }

    @PostMapping("add")
    public CommonResult<?> addPlan(
        @RequestBody Plan plan
    ) {
        try {
            planService.addPlan(plan);
        } catch(Exception e){
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success("添加成功");
    }
}
